﻿using System;

namespace Bookvar.UI.Commands
{
    public static class BookvarUICommands
    {
        private static readonly int commandsCount = Enum.GetNames( typeof ( CommandId ) ).Length;
        private static readonly BookvarCommand[] internalCommands = new BookvarCommand[commandsCount];

        public static BookvarCommand NextPresentationTopic
        {
            get { return EnsureCommand( CommandId.NextPresentationTopic ); }
        }

        public static BookvarCommand PreviousPresentationTopic
        {
            get { return EnsureCommand( CommandId.PreviousPresentationTopic ); }
        }

        public static BookvarCommand ExitPresentationMode
        {
            get { return EnsureCommand( CommandId.ExitPresentationMode ); }
        }

        public static BookvarCommand ShowAttachments
        {
            get { return EnsureCommand( CommandId.ShowAttachments ); }
        }

        public static BookvarCommand ShowNotes
        {
            get { return EnsureCommand( CommandId.ShowNotes ); }
        }

        public static BookvarCommand ShowHyperlink
        {
            get { return EnsureCommand( CommandId.ShowHyperlink ); }
        }

        public static BookvarCommand FocusSelectedTopic
        {
            get { return EnsureCommand( CommandId.FocusSelectedTopic ); }
        }

        private static BookvarCommand EnsureCommand( CommandId idCommand )
        {
            if ( ( idCommand < 0 ) || ( (int) idCommand >= commandsCount ) )
            {
                return null;
            }
            lock ( internalCommands.SyncRoot )
            {
                if ( internalCommands[ (int) idCommand ] == null )
                {
                    BookvarCommand newCommand =
                        new BookvarCommand( idCommand.ToString(), typeof ( BookvarUICommands ) );

                    internalCommands[ (int) idCommand ] = newCommand;
                }
            }
            return internalCommands[ (int) idCommand ];
        }

        private enum CommandId : byte
        {
            NextPresentationTopic = 0,
            PreviousPresentationTopic = 1,
            ShowAttachments = 2,
            ShowNotes = 3,
            ShowHyperlink = 4,
            FocusSelectedTopic = 5,
            ExitPresentationMode = 6
        }
    }
}